package com.dianrui.fmi.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dianrui.fmi.R;
import com.dianrui.fmi.activity.InformationActivity;
import com.dianrui.fmi.bean.Informtaion;
import com.dianrui.fmi.util.GlideUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class InformtationAdapter extends BaseAdapter {

    private List<Informtaion> list;
    private Context context;

    public InformtationAdapter(Context context, List<Informtaion> list) {
        this.list = list;
        this.context = context;
    }



    public void refresh(List<Informtaion> list) {
        this.list = list;
        this.notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_informtaion, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        if (null != list && list.size() > 0) {
            final Informtaion informtaion = list.get(position);
            holder.title.setText(informtaion.title);
            holder.createTime.setText(informtaion.create_time);
            GlideUtil.loadUserImageViewSize(context,informtaion.picture,holder.userIcon);
            holder.informationLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    parent.getContext().startActivity(new Intent(parent.getContext(), InformationActivity.class));
                }
            });
        }
        return convertView;
    }


    class ViewHolder {
        @BindView(R.id.picture)
        ImageView userIcon;
        @BindView(R.id.title)
        TextView title;
        @BindView(R.id.create_time)
        TextView createTime;
        @BindView(R.id.information_layout)
        LinearLayout informationLayout;


        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
